/*
 * Powered By [rapid-framework]
 * Web Site: http://www.rapid-framework.org.cn
 * Google Code: http://code.google.com/p/rapid-framework/
 * Since 2008 - 2012
 */

package com.tech15.task.action;

import static javacommon.util.extjs.Struts2JsonHelper.outXmlJson;

import java.io.InputStream;
import java.util.List;
import java.util.Map;

import javacommon.base.BaseStruts2Action;
import javacommon.util.ResInfo;
import cn.org.rapid_framework.page.Page;

import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;
import com.tech15.task.model.Zdxzdc;
import com.tech15.task.model.ZdxzdcCljl;
import com.tech15.task.service.ZdxzdcManager;
import com.tech15.task.vo.query.ZdxzdcQuery;

/**
 * @author 胡作正 email:zuozhenghu@sina.com
 * @version 1.0
 * @since 1.0
 */


public class ZdxzdcAction extends BaseStruts2Action implements Preparable,ModelDriven{
	//默认多列排序,example: username desc,createTime asc
	protected static final String DEFAULT_SORT_COLUMNS = "rq desc"; 
	
	//forward paths
	protected static final String LIST_JSP = "/task/zdxzdc_list.jsp";
	protected static final String MY_TASK_JSP = "/task/zdxzdc_my_list.jsp";
	protected static final String SEARCH_JSP= "/task/zdxzdc_search.jsp";
	protected static final String CREATE_JSP = "/task/zdxzdc_add.jsp";
	protected static final String EDIT_JSP = "/task/zdxzdc_edit.jsp";
	protected static final String PROCESS_JSP = "/task/zdxzdc_process.jsp";
	protected static final String SHOW_JSP = "/task/zdxzdc_show.jsp";
	protected static final String DETAIL_JSP = "/task/zdxzdc_detail.jsp";
	//redirect paths,startWith: !
	protected static final String LIST_ACTION = "!/zdxzdc/list.do";
	protected static final String MY_SEARCH_JSP = "/task/zdxzdc_my_search.jsp";
	
	private ZdxzdcManager zdxzdcManager;
	
	private Zdxzdc zdxzdc;
	java.lang.String id = null;
	private String[] items;

	public void prepare() throws Exception {
		if (isNullOrEmptyString(id)) {
			zdxzdc = new Zdxzdc();
		} else {
			zdxzdc = (Zdxzdc)zdxzdcManager.getById(id);
		}
	}
	
	/** 增加setXXXX()方法,spring就可以通过autowire自动设置对象属性,注意大小写 */
	public void setZdxzdcManager(ZdxzdcManager manager) {
		this.zdxzdcManager = manager;
	}	
	
	public Object getModel() {
		return zdxzdc;
	}
	
	public void setId(java.lang.String val) {
		this.id = val;
	}

	public void setItems(String[] items) {
		this.items = items;
	}
	
	/** 执行搜索 */
	public String list() {
		ZdxzdcQuery query = newQuery(ZdxzdcQuery.class,DEFAULT_SORT_COLUMNS);
		
		Page page = zdxzdcManager.findPage(query);
		savePage(page,query);
		return LIST_JSP;
	}
	
	
	/** 执行搜索 */
	public String export() {
		ZdxzdcQuery query = newQuery(ZdxzdcQuery.class,DEFAULT_SORT_COLUMNS);
		reportStream = zdxzdcManager.searchExport(query);
		if(reportStream != null){
			try{
				String fileName = "重大行政督查事项.xls";
				this.filename = new String(fileName.getBytes(), "ISO8859-1");
			} catch (Exception e) {
			    e.printStackTrace();
			}
			return "sjdc";
		}else{
			Map<String, Object> resInfo = null;
			resInfo = ResInfo.getResInfo("",false, false, 2, "无任何导出结果","");
			outXmlJson(resInfo);
		}
		return null;
	}
	
	public InputStream getReportStream() {
		return reportStream;
	}
	
	/** 执行搜索 */
	public String search() {
		ZdxzdcQuery query = newQuery(ZdxzdcQuery.class,DEFAULT_SORT_COLUMNS);
		query.setSpzt(1);
		query.setLdcx(1);
		//query.setZxzt(1);
		//query.setSpr(getLoginInfo().getUserid());
		Page page = zdxzdcManager.findPage(query);
		savePage(page,query);
		return SEARCH_JSP;
	}
	
	public String myTask() {
		ZdxzdcQuery query = newQuery(ZdxzdcQuery.class,DEFAULT_SORT_COLUMNS);
		/*query.setShr(getLoginInfo().getUserid());
		query.setFgld(getLoginInfo().getUserid());
		query.setZxr(getLoginInfo().getUserid());*/
		
		query.setZtbs(0);
		this.zdxzdc.setZtbs(0);
		
		query.setUserid(getLoginInfo().getUserid());
		Page page = zdxzdcManager.myTask(query);
		savePage(page,query);
		return MY_TASK_JSP;
	}
	
	public String mySearch() {
		ZdxzdcQuery query = newQuery(ZdxzdcQuery.class,DEFAULT_SORT_COLUMNS);
		
		query.setZtbs(1);
		
		query.setUserid(getLoginInfo().getUserid());
		Page page = zdxzdcManager.myTask(query);
		savePage(page,query);
		return MY_SEARCH_JSP;
	}
	
	/** 查看对象*/
	public String show() {
		return SHOW_JSP;
	}
	
	/** 查看对象*/
	public String detail() {
		return DETAIL_JSP;
	}
	
	/** 查看对象*/
	public String detail2() {
		List<ZdxzdcCljl> cljlList = zdxzdcManager.searchWcjlById(id);
		this.zdxzdc.setCljlList(cljlList);
		return DETAIL_JSP;
	}
	
	/** 进入新增页面*/
	public String create() {
		return CREATE_JSP;
	}
	
	/** 保存新增对象 */
	public void save() {
		Map<String, Object> resInfo = null;
		try{
			zdxzdc.setQcr(getLoginInfo().getUserid());
			zdxzdc.setQcrxm(getLoginInfo().getRealname());
			zdxzdcManager.saveXzdc(zdxzdc);
			resInfo = ResInfo.getResInfo("",false, false, 2, this.OPERATE_SUCCESS,"");
		}catch(Exception e){
			e.printStackTrace();
			resInfo = ResInfo.getResInfo("",false, false, 1, this.OPERATE_FAILED,"");
		}
		outXmlJson(resInfo);
	}
	/**审批分配*/
	public void shfp() {
		Map<String, Object> resInfo = null;
		try{
			zdxzdcManager.shfp(zdxzdc);
			resInfo = ResInfo.getResInfo("",false, false, 2, this.OPERATE_SUCCESS,"");
		}catch(Exception e){
			e.printStackTrace();
			resInfo = ResInfo.getResInfo("",false, false, 1, this.OPERATE_FAILED,"");
		}
		outXmlJson(resInfo);
	}
	
	/**执行分配*/
	public void zxfp() {
		Map<String, Object> resInfo = null;
		try{
			zdxzdcManager.zxfp(zdxzdc);
			resInfo = ResInfo.getResInfo("",false, false, 2, this.OPERATE_SUCCESS,"");
		}catch(Exception e){
			e.printStackTrace();
			resInfo = ResInfo.getResInfo("",false, false, 1, this.OPERATE_FAILED,"");
		}
		outXmlJson(resInfo);
	}
	
	/**执行处理*/
	public void zxcl() {
		Map<String, Object> resInfo = null;
		try{
			zdxzdcManager.zxcl(zdxzdc);
			resInfo = ResInfo.getResInfo("",false, false, 2, this.OPERATE_SUCCESS,"");
		}catch(Exception e){
			e.printStackTrace();
			resInfo = ResInfo.getResInfo("",false, false, 1, this.OPERATE_FAILED,"");
		}
		outXmlJson(resInfo);
	}
	
	/**执行审核*/
	public void zxsh() {
		Map<String, Object> resInfo = null;
		try{
			zdxzdcManager.zxsh(zdxzdc);
			resInfo = ResInfo.getResInfo("",false, false, 2, this.OPERATE_SUCCESS,"");
		}catch(Exception e){
			e.printStackTrace();
			resInfo = ResInfo.getResInfo("",false, false, 1, this.OPERATE_FAILED,"");
		}
		outXmlJson(resInfo);
	}
	
	/**执行审核退回*/
	public void zxshth() {
		Map<String, Object> resInfo = null;
		try{
			zdxzdcManager.zxshth(zdxzdc);
			resInfo = ResInfo.getResInfo("",false, false, 2, this.OPERATE_SUCCESS,"");
		}catch(Exception e){
			e.printStackTrace();
			resInfo = ResInfo.getResInfo("",false, false, 1, this.OPERATE_FAILED,"");
		}
		outXmlJson(resInfo);
	}
	
	
	/**进入更新页面*/
	public String edit() {
		return EDIT_JSP;
	}
	
	public String process(){
		if(!isNullOrEmptyString(this.zdxzdc.getJlid())){
			ZdxzdcCljl cljl = zdxzdcManager.getJlByClid(this.zdxzdc.getJlid());
			this.zdxzdc.setCljl(cljl);
		}
		
		return PROCESS_JSP;
	}
	
	/**保存更新对象*/
	public void update() {
		Map<String, Object> resInfo = null;
		try{
			zdxzdcManager.update(this.zdxzdc);
			resInfo = ResInfo.getResInfo("",false, false, 2, this.OPERATE_SUCCESS,"");
		}catch(Exception e){
			e.printStackTrace();
			resInfo = ResInfo.getResInfo("",false, false, 1, this.OPERATE_FAILED,"");
		}
		outXmlJson(resInfo);
	}
	
	/**删除对象*/
	public void delete() {
		Map<String, Object> resInfo = null;
		try {
			zdxzdcManager.removeById(id);
			resInfo = ResInfo.getResInfo("",false, false, 2, this.OPERATE_SUCCESS,"");
		} catch (Exception e) {
			e.printStackTrace();
			resInfo = ResInfo.getResInfo("",false, false, 1,this.OPERATE_FAILED,"");
		}		
		outXmlJson(resInfo);
	}
	
	
	private String opt;
	private InputStream reportStream;
	private String filename;

	public String getFilename() {
		return filename;
	}

	public void setFilename(String filename) {
		this.filename = filename;
	}

	public String getOpt() {
		return opt;
	}

	public void setOpt(String opt) {
		this.opt = opt;
	}
}
